package com.tn.exam.action;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.stereotype.Component;

import com.tn.exam.action.base.BaseAction;
import com.tn.exam.constant.ExamConstant;
import com.tn.exam.entity.InfoSysUser;
import com.tn.exam.service.UserService;
import com.tn.exam.util.taoyf.enchar.MD5;

@Component
@ParentPackage("basePackage")
@Results({
	 @Result(name="input", location="/login.jsp"),
	 @Result(name="index", location="/WEB-INF/content/main.jsp"),
	 @Result(name="tomain",type="redirectAction" ,location="index"),
	 @Result(name="success", location="/WEB-INF/content/login-success.jsp")
	})
public class LoginAction extends BaseAction{
	private String username;
	private String password;
	private String message;
	private UserService userService;
	
	@Action("login")
	public String login() throws Exception {
		if(StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)){
			InfoSysUser sysUser = userService.getSysUser(username);
			if(sysUser != null && sysUser.getSysUserPassword().equals(MD5.MD5Encode(password) )){
				this.getHttpSession().setAttribute(ExamConstant.SESSION_USER_KEY, sysUser.getSysUserName());
				return "tomain";
			}
		}
		message = "用户名或者密码错误!";
		return "input";
	}
	
	@Action("index")
	public String index() throws Exception {
		return "index";
	}

	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	
}
